package com.xiaomiMall.mange.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.xiaomiMall.mange.domain.*;
import com.xiaomiMall.mange.domain.vo.Order;
import com.xiaomiMall.mange.service.Impl.MallTokenServiceImpl;
import com.xiaomiMall.mange.service.Impl.MallUserServiceImpl;
import com.xiaomiMall.mange.service.XiaoMiHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

//开启异步
@EnableAsync
@RestController
@RequestMapping("/api/headerTop/list")
public class XIaoMiHeaderController {
    @Autowired
    private XiaoMiHeaderService xiaoMiHeader;

    @Autowired
    private MallTokenServiceImpl mallTokenService;

    @Autowired
    private MallUserServiceImpl userService;

    @RequestMapping("/getTopList")
    public List<XiaoMiTop> getXiaoMiTop(){
        return xiaoMiHeader.selectXiaoMiTop();
    }

    @RequestMapping("/getNavList")
    public List<XiaoMiNav> getXiaoMiNav(){
        return xiaoMiHeader.selectXiaoMiNav();
    }

    @RequestMapping("/getNavPicList")
    public List<XiaoMiNavPic> getXiaoMiPic(){
        return xiaoMiHeader.selectXiaoMiNavPic();
    }


    /**添加购物车 */
    @PostMapping("/add")
    public AjaxResult addCart(HttpServletRequest request,@RequestBody HashMap<String, Long> data)
    {
        AjaxResult ajax = AjaxResult.success();
        MallLoginUser mallLoginUser = mallTokenService.getLoginUser(request);
        if (StringUtils.isNotNull(mallLoginUser)){
            MallUser User = mallLoginUser.getMallUser();
            String message = xiaoMiHeader.addCart(User.getId(),data.get("uid")) !=0 ? "添加成功": "";
            ajax.put("message", message);
        } else {
            ajax = AjaxResult.error(401,"登陆状态已过期");
        }
        return ajax;
    }

    /** 查询购物车 */
    @GetMapping("/allCart")
    public AjaxResult selectAll(HttpServletRequest request)
    {
        AjaxResult ajax = null;
        MallLoginUser mallLoginUser = mallTokenService.getLoginUser(request);
        if (StringUtils.isNotNull(mallLoginUser)){
            ajax = AjaxResult.success();
            MallUser User = mallLoginUser.getMallUser();
            ajax.put("message", xiaoMiHeader.selectAll(User.getId()));
        }else {
            ajax = AjaxResult.error(401,"登陆状态已过期");
        }
        return ajax;
    }

    /** 计数器改变 */
    @PostMapping("/updateNumber")
    public AjaxResult updateNumber (@RequestBody HashMap<String, Long> data)
    {
        boolean result = xiaoMiHeader.updateNumber(data.get("amount"), data.get("id"));
        System.out.println(data);
        if (result){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /** 删除购物车 */
    @PostMapping("/deleteCart")
    public AjaxResult deleteCart(@RequestBody HashMap<String, Long> data){
        int result =  xiaoMiHeader.deleteCart(data.get("id"));
        if (result != 0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /** 添加待支付订单 */
    @PostMapping("/addOrder")
    public AjaxResult addOrder(HttpServletRequest request,@RequestBody HashMap<String,Object> data)
    {
        AjaxResult ajax = AjaxResult.success();
        MallLoginUser loginUser = mallTokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser)) {
            List<Order> orderList = userService.creatOrder((List<Integer>) data.get("ids"), Long.valueOf(String.valueOf(data.get("address_id"))));
            Boolean succ = userService.insertOrder(orderList);
            if (succ) {
                xiaoMiHeader.deleteCart((List<Long>) data.get("ids"));
            }
            ajax.put("succ", succ);
            ajax.put("orderNum", orderList.get(0).getOrderNumber());
        } else {
            ajax = AjaxResult.error(401,"登陆状态已过期");
        }
        return ajax;
    }

}
